package com.zlq.cn.service.juhe;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zlq.cn.config.OkhttpConfig;
import com.zlq.cn.entity.JuheZw;
import com.zlq.cn.entity.JuheZwContent;
import com.zlq.cn.mapper.juhe.JuheZwMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import okhttp3.HttpUrl;
import okhttp3.Request;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 聚合作文 服务实现类
 * </p>
 *
 * @author zhoucl
 * @since 2023-12-13
 */
@Slf4j
@Service
public class JuheZwService extends ServiceImpl<JuheZwMapper, JuheZw> {
    public static final String KEY = "408ab788db178c9e81fadf1289707631";

//    @PostConstruct
//    public void initZwList() {
//        int page = 1;
//        for (int i = 1000; i < 2000; i++) {
//            try {
//                HttpUrl.Builder builder = HttpUrl.parse("http://zuowen.api.juhe.cn/zuowen/baseList")
//                        .newBuilder()
//                        .addQueryParameter("key", KEY)
//                        .addQueryParameter("page", (page + i) + "");
//                Request request = new Request.Builder()
//                        .url(builder.build())
//                        .get()
//                        .addHeader("Content-Type", "application/x-www-form-urlencoded")
//                        .build();
//                String stringBody = OkhttpConfig.okHttpClient.newCall(request).execute().body().string();
//                JSONObject body = JSON.parseObject(stringBody);
//                Integer errorCode = body.getInteger("error_code");
//                if (errorCode != null && errorCode == 0) {
//                    List<JuheZw> juheZws = JSON.parseArray(body.getJSONObject("result").getString("list"), JuheZw.class);
//                    super.saveBatch(juheZws);
//                }
//                System.out.println("处理第：" + (page + i) + "页");
//            } catch (Exception e) {
//                log.error("请求聚合作文详情失败", e);
//                e.printStackTrace();
//            }
//        }
//    }

    public JSONObject list(JuheZw param) {
        try {
            HttpUrl.Builder builder = HttpUrl.parse("http://zuowen.api.juhe.cn/zuowen/baseList")
                    .newBuilder()
                    .addQueryParameter("key", KEY);
            if (Objects.nonNull(param.getGradeId())) {
                builder.addQueryParameter("gradeId", String.valueOf(param.getGradeId()));
            }
            if (Objects.nonNull(param.getTypeId())) {
                builder.addQueryParameter("typeId", String.valueOf(param.getTypeId()));
            }
            if (Objects.nonNull(param.getWordId())) {
                builder.addQueryParameter("wordId", String.valueOf(param.getWordId()));
            }
            if (Objects.nonNull(param.getLevel())) {
                builder.addQueryParameter("level", String.valueOf(param.getLevel()));
            }
            Request request = new Request.Builder()
                    .url(builder.build())
                    .get()
                    .addHeader("Content-Type", "application/x-www-form-urlencoded")
                    .build();
            String stringBody = OkhttpConfig.okHttpClient.newCall(request).execute().body().string();
            JSONObject body = JSON.parseObject(stringBody);
            Integer errorCode = body.getInteger("error_code");
            if (errorCode != null && errorCode == 0) {
                return body.getJSONObject("result");
            }
            throw new IllegalStateException(body.getString("reason"));
        } catch (Exception e) {
            log.error("请求聚合作文详情失败", e);
            e.printStackTrace();
        }
        return null;
    }
}
